package com.androidhuman.example.mapsv2example;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.Date;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.widget.Toast;

public class UsingServer {
	static int last_id;
	static Timestamp  last_created;
	static Timestamp  last_created_c;
	static int last_like_num;
	
	String from = "2013-04-08 10:10:10";
	Date d = java.sql.Date.valueOf(from);
	
	public static Bitmap getImageFromURL(String imageURL) {
		Bitmap imgBitmap = null;
		HttpURLConnection conn = null;
		BufferedInputStream bis = null;

		try {
			URL url = new URL(imageURL);
			conn = (HttpURLConnection) url.openConnection();
			conn.connect();

			int nSize = conn.getContentLength();
			bis = new BufferedInputStream(conn.getInputStream(), nSize);
			imgBitmap = BitmapFactory.decodeStream(bis);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (bis != null) {
				try {
					bis.close();
				} catch (IOException e) {
				}
			}
			if (conn != null) {
				conn.disconnect();
			}
		}
		

		return imgBitmap;
	}
	
	public static String[][] getDataFromServer_E_detail(int event_id, int option)
	{
		String rowString;
		try {
			rowString = HttpRequest.get(
					"http://dev.ts.hs.kr/trend/reporters_get_events_detail.php",
					true, "event_id", event_id,
					"option", option).body();//option은 1또는 2
		} catch (Exception e) {
			return null;
		}
		return outPutData(rowString, option);
	}
	
	public static String[][] getDataFromServer(String option_type, int option_near_or_all, 
			int option_hot_or_new_or_event,  boolean option_be_left_undone) 
	{
		String rowString;

		if(option_type.compareTo("comment")==0)
		{
			return getDataFromServer_Comment(option_near_or_all,option_hot_or_new_or_event,option_be_left_undone);
		}
		else if(option_type.compareTo("report")==0)
		{
			return getDataFromServer_Report_NotMap(option_near_or_all,option_hot_or_new_or_event,option_be_left_undone);
		}
		else if(option_type.compareTo("report_map")==0)
		{
			return getDataFromServer_Report_Map(option_hot_or_new_or_event,option_be_left_undone);
		}
		return null;
	}

	public static String[][] getDataFromServer_Comment( int option_near_or_all, 
			int option_hot_or_new_or_event, boolean option_be_left_undone) {
		String rowString;
		int type = 0;
		if (option_hot_or_new_or_event == 2)
			type = 1;
		try {
			rowString = HttpRequest.get(
					"http://dev.ts.hs.kr/trend/reporters_get_comments.php",
					true, "article_event_id", option_near_or_all,
					"article_or_event", type).body();
		} catch (Exception e) {
			return null;
		}
		return outPutData(rowString, -1);
	}

	public static String[][] getDataFromServer_Report_Map(  
			int option_hot_or_new_or_event, boolean option_be_left_undone)
	{
		String rowString;
		try {
			rowString = HttpRequest
					.get("http://dev.ts.hs.kr/trend/reporters_get_articles_events_map.php",
							true,
							"option_hot_or_new_or_event",
							option_hot_or_new_or_event, 
							"latitude1", MapActivity.location[0][0],
							"latitude2", MapActivity.location[2][0],
							"longitude1", MapActivity.location[0][1],
							"longitude2", MapActivity.location[2][1]).body();}
		catch (Exception e) {
			return null;
		}
		return outPutData(rowString, option_hot_or_new_or_event);
	}
	
	public static String[][] getDataFromServer_Report_NotMap( int option_near_or_all, 
			int option_hot_or_new_or_event, boolean option_be_left_undone)
	{
		String rowString;
		if(!option_be_left_undone){
		try {
			rowString = HttpRequest
					.get("http://dev.ts.hs.kr/trend/reporters_get_articles_events.php",
							true, "option_near_or_all", option_near_or_all,
							"option_hot_or_new_or_event",
							option_hot_or_new_or_event, "latitude",
							GPSTracker.latitude, "longitude",
							GPSTracker.longitude).body();
		} catch (Exception e) {
			return null;
		}
		return outPutData(rowString, option_hot_or_new_or_event);}
		else
		{
			try {
				if(option_hot_or_new_or_event==0)
				rowString = HttpRequest
						.get("http://dev.ts.hs.kr/trend/reporters_get_articles_events_more_hot.php",
								true, "option_near_or_all", option_near_or_all,
								"option_hot_or_new_or_event",
								option_hot_or_new_or_event, "latitude",
								GPSTracker.latitude, "longitude",
								GPSTracker.longitude,
								"last_id", last_id,
								"last_like_num", last_like_num).body();
				else if(option_hot_or_new_or_event==1)
				{
					rowString = HttpRequest
							.get("http://dev.ts.hs.kr/trend/reporters_get_articles_events_more_new.php",
									true, "option_near_or_all", option_near_or_all,
									"option_hot_or_new_or_event",
									option_hot_or_new_or_event, "latitude",
									GPSTracker.latitude, "longitude",
									GPSTracker.longitude,
									"last_id", last_id,
									"last_created", last_created).body();
				}
				else if(option_hot_or_new_or_event==2)
				{
					rowString = HttpRequest
							.get("http://dev.ts.hs.kr/trend/reporters_get_articles_events_more_event.php",
									true, "option_near_or_all", option_near_or_all,
									"option_hot_or_new_or_event",
									option_hot_or_new_or_event, "latitude",
									GPSTracker.latitude, "longitude",
									GPSTracker.longitude,
									"last_id", last_id,
									"last_created", last_created).body();
				}
			} catch (Exception e) {
				return null;
			}
			return null;
		}
	}
	
	public static String[][] getDataFromServer(int option_near_or_all, int option_hot_or_new_or_event) {
		return getDataFromServer_Report_NotMap(option_near_or_all,option_hot_or_new_or_event,false);
	}
	
	public static String[][] outPutData(String rowString, int option_hot_or_new_or_event)
	{
		boolean isok = false;
		//		Toast toast = Toast.makeText(this,
		//				String.valueOf(rowString.contains("閠")), Toast.LENGTH_SHORT);
		//		toast.show();
		String[] tmpRow = new String[2];
		tmpRow = rowString.split("墸墸");
		// rowString=tmpRow[1];
		if (rowString.endsWith("墸")) {
			isok = false;
		} else {
			isok = true;
		}
		// Toast toast1 = Toast.makeText(this, String.valueOf(tmpRow[1]==null),
		// Toast.LENGTH_SHORT);
		// toast1.show();
		if (rowString.contains("閠") && isok) {
			isok = true;
			rowString = tmpRow[1];
		} else {
			isok = false;
		}
		//		Toast toast1 = Toast.makeText(this, String.valueOf(isok),
		//				Toast.LENGTH_SHORT);
		//		toast1.show();
		if (isok) {

			// 문자열로 된 데이터 잘라 배열에 넣는다
			String[] row_event_article = rowString.split("彁");

			// 글의 종류에 따라 배열의 길이를 다르게
			int length = 0;
			
			if (option_hot_or_new_or_event == 0) {
				length = 12;
			} else
				length = 13;
			if(option_hot_or_new_or_event<0)
				length=7;
			String[][] event_article = new String[row_event_article.length][length];

			// 한번 더 끊는다
			int i = 0;
			while (i < row_event_article.length) {
				String[] tmp = row_event_article[i].split("閠");
				event_article[i] = tmp;
				i++;
			}
			if(option_hot_or_new_or_event==1 || option_hot_or_new_or_event==2)
			{
				//Toast toast=Toast.makeText(getApplicationContext(), TextChecker.purifyString(event_article[row_event_article.length-1][4]), Toast.LENGTH_SHORT);
				last_created=java.sql.Timestamp.valueOf(TextChecker.purifyString(event_article[row_event_article.length-1][4]));
				Log.i("KKL", TextChecker.purifyString(event_article[row_event_article.length-1][4]));
			}
			else if(option_hot_or_new_or_event<0)
			{
				last_created_c=java.sql.Timestamp.valueOf(TextChecker.purifyString(event_article[row_event_article.length-1][3]));
			}
			else if(option_hot_or_new_or_event==0)
			{
				last_like_num=Integer.parseInt(TextChecker.purifyString(event_article[row_event_article.length-1][6]));
			}
			last_id=Integer.parseInt(TextChecker.purifyString(event_article[row_event_article.length-1][0]));
			return event_article;
		}
		return null;
	}

	public static String likeUp(int id, int type)
	{
//		Toast toast=Toast.makeText(getBaseContext(), "실행은 됨", Toast.LENGTH_LONG);
//		toast.show();
//		Toast toast1=Toast.makeText(this, Integer.toString(id), Toast.LENGTH_LONG);
//		toast1.show();
		return increaseAndGetLikeNum(type, id);
	}
	
	public static String increaseAndGetLikeNum(int type, int id)
	{
		String str;
		if(type==1) type=0;
		try{
			if(type==0)
				str=HttpRequest.get(
						"http://dev.ts.hs.kr/trend/reporters_increase_like_num_A.php", true, 
						"id", id
						)
						.body();

			else
				str=HttpRequest.get(
						"http://dev.ts.hs.kr/trend/reporters_increase_like_num_E.php", true, 
						"id", id
						)
						.body();

		}catch (Exception e) {
			//Toast.makeText(this, "인터넷 연결 오류입니다.", Toast.LENGTH_SHORT).show();
			return null;
		}
		str=TextChecker.purifyString(str);
		return str;
	}
}
